package com.bootdo.ocr.baidu;

import com.alibaba.fastjson.JSON;
import com.bootdo.exception.BizException;
import com.bootdo.ocr.DpiType;
import com.bootdo.ocr.FileType;
import com.bootdo.ocr.FileUrlDTO;
import com.bootdo.ocr.MatchType;
import com.bootdo.ocr.baidu.dto.BaiduOcrResult;
import com.bootdo.ocr.xfyun.XfYunOcrService;
import com.bootdo.ocr.xfyun.dto.XfYunOcrResult;
import com.bootdo.utils.PDFToIMGUtil;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class BaiDuOcrServiceTest {

    String apiKey = "tGEDAGQBdMEk2EhgGzYXf4Hr";
    String secretKey = "ubGEcI8tF1F9hdpm7gzbgmeiou25bIDB";
    String appId = "17005522";
    BaiDuOcrService service = new BaiDuOcrService(apiKey, secretKey, appId);

    @org.junit.Test
    public void updateFileByDir() throws IOException {
        final List<BaiduOcrResult> list = service.validateFileByDirAndPattern("D:\\document\\aaa\\GB 10051.3-1988.pdf", FileType.pdf, /*BaiDuOcrService.GB_PATTERN*/ "GB", DpiType.D500);
        final List<BaiduOcrResult> matchs = list.stream().filter(obj -> obj.getMatchType() == MatchType.MATCH || obj.getMatchType() == MatchType.PARTICAL).collect(Collectors.toList());
        final List<BaiduOcrResult> failMatchs = list.stream().filter(obj ->  obj.getMatchType() == MatchType.NOTMATCH).collect(Collectors.toList());
        System.out.println(matchs + " size == >>" + matchs.size());
        System.out.println(JSON.toJSONString(failMatchs));
    }
    @org.junit.Test
    public void testFetchOcr() {
        FileUrlDTO dto = FileUrlDTO.initFilePath(Lists.newArrayList("D:\\document\\mix\\FD 008-2011.png"), "FD");
        service.fetchOcrDataByFileUrlDTO(dto);
    }
    @org.junit.Test
    public void grouopForder() throws IOException{
        List<String> fileList = new ArrayList<>();
        String path = "D:\\document\\电子文本测试\\电子文本测试";
        FileType type = FileType.pdf;
        XfYunOcrService.getFileUrl(path, fileList, type);
        final Map<String, HashSet<String>> stringHashSetMap = XfYunOcrService.groupFiles(fileList);
        stringHashSetMap.forEach((pattern, paths) -> {
            paths.forEach(pathObj -> {
                try {
                    final List<BaiduOcrResult> baiduOcrResults = service.validateFileByDirAndPattern(pathObj, FileType.pdf, pattern, DpiType.D500);
                    System.out.println(baiduOcrResults + " size == >>" + baiduOcrResults.size());
                    System.out.println(JSON.toJSON(baiduOcrResults));
                }catch (BizException e) {
                    e.printStackTrace();
                }
            });
        });
    }

    @Test
    public void pdf2Png() {
        PDFToIMGUtil.pdf2PngPath("D:\\document\\mix\\UNDO\\GB 71-1985.pdf", "D:\\document\\mix\\UNDO\\a.png", DpiType.D500.getDpiVal());
    }
}